package form;

import java.util.List;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import entity.Customer;
import java.util.HashMap;
import session.CustomerFacade;

public final class CustomerForm
{
    private static void emailIsValid(String email, CustomerFacade cf) throws FormValidationException
    {		
        if(!email.equals(""))
        {
            if(!email.matches("([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)"))
            {
                throw new FormValidationException("Please type a valid email.");
            }
            else
            {
                Customer c = null;
                try
                {
                    c = cf.findByEmail(email);             
                }
                catch(Exception e)
                {
                }
                if(c != null)
                {
                    throw new FormValidationException("This email is already registered.");                
                }
            }
        } 
        else
        {
                throw new FormValidationException("Please type an email.");
        }
    }
	
    private static void passwordIsValid(String password, String confirmation) throws FormValidationException
    {
        if(password != null && confirmation != null)
        {
            if(!password.equals(confirmation))
            {
                throw new FormValidationException("Password and confirmation password are not equals.");
            } 
            else if(password.length() < 6)
            {
                throw new FormValidationException("Passwords must have 6 characters.");
            }
        } 
        else
        {
            throw new FormValidationException("Please type the two passwords.");
        }
    }
    
    private static void phoneIsValid(String phone) throws FormValidationException
    {
        if(phone != null)
        {
            if(phone.length() < 6)
            {
                throw new FormValidationException("Phone number must have 6 characters.");
            }
        } 
        else
        {
            throw new FormValidationException("Please type a phone number.");
        }
    }     
        
    public static HashMap<String, String> validateForm(HttpServletRequest request, CustomerFacade cf)
    {
        HashMap<String, String> errors = new HashMap<String, String>();
        
        String fname = request.getParameter("fname");
        String lname = request.getParameter("lname");
        String address = request.getParameter("address");
        
        try {
             if(fname == null || fname.equals("")) {
                throw new FormValidationException("Please enter your first name.");
             }  
        } catch(FormValidationException fve) {
            errors.put( "fname", fve.getMessage() );
        }
        
        try {
             if(lname == null || lname.equals("")) {
                throw new FormValidationException("Please enter your last name.");
             }  
        } catch(FormValidationException fve) {
            errors.put( "lname", fve.getMessage() );
        }
        
        try {
             if(address == null || address.equals("")) {
                throw new FormValidationException("Please enter your address.");
             }  
        } catch(FormValidationException fve) {
            errors.put( "address", fve.getMessage() );
        }
        
        try {
            emailIsValid(request.getParameter("email"), cf);
        } catch(FormValidationException fve) {
            errors.put( "email", fve.getMessage() );
        }
        
        try {
            passwordIsValid(request.getParameter("password"), request.getParameter("passwordc"));
        } catch(FormValidationException fve) {
            errors.put( "password", fve.getMessage() );
        }
        
        try {
            phoneIsValid(request.getParameter("phone"));
        } catch(FormValidationException fve) {
            errors.put( "phone", fve.getMessage() );
        }
        
        return errors;
    }
    
    public static HashMap<String, String> updateForm(HttpServletRequest request, CustomerFacade cf)
    {
        HashMap<String, String> errors = new HashMap<String, String>();
        
        String address = request.getParameter("address");
        
        try {
             if(address == null || address.equals("")) {
                throw new FormValidationException("Please enter your address.");
             }  
        } catch(FormValidationException fve) {
            errors.put( "address", fve.getMessage() );
        }
        
        try {
            phoneIsValid(request.getParameter("phone"));
        } catch(FormValidationException fve) {
            errors.put( "phone", fve.getMessage() );
        }
        
        return errors;
    }
}